package org.adream.account.service;

import org.adream.account.dao.DonornDao;
import org.adream.account.entity.DonornEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DonornService {
	
	@Autowired
	private DonornDao donornDao;
	
	private final static Logger logger = LoggerFactory.getLogger(DonornService.class);
	
	/**
	 * 新增或更新捐赠方
	 * @param donornEntity
	 * @return
	 */
	public ResultModel<String> addOrUpdateDonorn(DonornEntity donornEntity) {
		//根据id查询捐赠方
		DonornEntity donorn = donornDao.queryDonornById(donornEntity.getId());
		if(donorn == null) {
			donornEntity.setDr(Constant.ONE);
			int addDonornResult = donornDao.addDonorn(donornEntity);
			if(addDonornResult == Constant.ZERO) {
				logger.warn("新增捐赠方失败,数据库错误:(");
				return new ResultModel<String>(ResultModel.ERR_SYS, "新增捐赠方失败,请联系管理员", null);
			}
			return new ResultModel<String>(ResultModel.SUCCESS, "新增捐赠方成功", null);
		}
		Utils.copyPropertiesIgnoreNull(donornEntity, donorn);
		int result = donornDao.updateDonorn(donorn);
		if(result == 0) {
			logger.warn("新增或更新捐赠方,数据库错误,id:{}", donornEntity.getId());
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, "更新捐赠方成功", null);
	}
}